package study;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class ListS {
public static void main(String[] args) {  
		String[] str = new String[5];
		//新建数组
		for(int i = 0;i<str.length;i++){
			str[i] = "test"+i;
		}

		for(int i = 0;i<str.length;i++){
			System.out.println("原始数组:" + str[i]);
		}
		
		//删除第三位
        String[] newStr = new String[4];
        int j = 0;
		for(int i = 0;i<str.length;i++){
			if ("test2".equals(str[i])){
				j = i;
				continue;
			}
			newStr[j]= str[i];
			j++;
		}

		for(int i = 0;i<newStr.length;i++){
			System.out.println("删除第三位:" +newStr[i]);
		}
		//修改最后一位

		for(int i = 0;i<str.length;i++){
			if ("test4".equals(str[i])){
				str[i] = "str3";
			}
		}

		for(int i = 0;i<str.length;i++){
			System.out.println("修改:" +str[i]);
		}
		
		//查出第五个元素

		for(int i = 0;i<str.length;i++){
			if (i==4){
				System.out.println("第五个元素:" +str[i]);
				break;
			}
		}
		
		//查出元素"test3"的索引

		for(int i = 0;i<str.length;i++){
			if ("test3".equals(str[i])){
				System.out.println("test3的索引是:" +i);
				break;
			}
		}
		//数组合集
		String[] str1 = new String[4];
		//新建数组
		for(int i = 0;i<str1.length;i++){
			str1[i] = "str"+i;
		}
		
		for(int i = 0;i<str1.length;i++){
			System.out.println("新数组:" +str1[i]);
		}

		String[] result=new String[str.length+str1.length]; 
		
		System.arraycopy(str, 0, result, 0, str.length);
		System.arraycopy(str1, 0, result, str.length, str1.length);

		for(int i = 0;i<result.length;i++){
			System.out.println("合并后数组:" +result[i]);
		}
		//数组交集
		int z=0;
		for(int i = 0;i<str.length;i++)
		{
			for(int k = 0;k<str1.length;k++)
			{
				if(str[i].equals(str1[k]))
				{
					z++;
				}
			}
		}
		String[] result_union = new String[z];
		int y = 0;
		for(int i = 0;i<str.length;i++)
		{
			for(int k = 0;k<str1.length;k++)
			{
				if(str[i].equals(str1[k]))
				{   
					result_union[y] =str[i];
					y++;
				}
			}
		}
		
		for(int i = 0;i<result_union.length;i++){
			System.out.println("交集:" +result_union[i]);
		}
		//数组并集
		
		String[] result_ins = diff(result);
		for(int i = 0;i<result_ins.length;i++){
			System.out.println("并集:" +result_ins[i]);
		}
		
	}
	//并集用
	private static String[] diff(String[] arr) {
	    int index = 0;
	    for (int i = 0; i < arr.length; i++) {
	        if (hasSame(arr, i)) {
	            continue;
	        }
	        arr[index++] = arr[i];
	    }
	    return subArray(arr, index);
	}
	
	private static String[] subArray(String[] arr, int size) {
		String[] arr2 = new String[size];
	    for (int i = 0; i < arr2.length; i++) {
	        arr2[i] = arr[i];
	    }
	    return arr2;
	}
	
	private static boolean hasSame(String[] arr, int index) {
	    for (int i = 0; i < index; i++) {
	        if (arr[index].equals(arr[i])) {
	            return true;
	        }
	    }
	    return false;
	}
}
